package com.adv.ems.manager.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.adv.ems.db.mapper.EnergyDataOriginalMapper;
import com.adv.ems.db.entity.EnergyDataOriginalDO;
import com.adv.ems.manager.EnergyDataOriginalManager;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class EnergyDataOriginalManagerImpl extends ServiceImpl<EnergyDataOriginalMapper, EnergyDataOriginalDO> implements EnergyDataOriginalManager {
    @Override
    public List<EnergyDataOriginalDO> loadUnDealData(Long lastId, Integer pullOriginalDataSize) {
        return baseMapper.loadUnDealData(lastId, pullOriginalDataSize);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateDealState(Long id, Date colDate, String state, String remark) {
        LambdaQueryWrapper<EnergyDataOriginalDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EnergyDataOriginalDO::getId, id);
        wrapper.eq(EnergyDataOriginalDO::getColDate, colDate);
        wrapper.isNull(EnergyDataOriginalDO::getState);

        EnergyDataOriginalDO record = new EnergyDataOriginalDO();
        record.setState(state);
        record.setRemark(remark);
        return update(record, wrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean batchUpdateDealState(Long from, Long end, String state, String remark) {
        LambdaQueryWrapper<EnergyDataOriginalDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.ge(EnergyDataOriginalDO::getId, from);
        wrapper.le(EnergyDataOriginalDO::getId, end);
        wrapper.isNull(EnergyDataOriginalDO::getState);

        EnergyDataOriginalDO record = new EnergyDataOriginalDO();
        record.setState(state);
        record.setRemark(remark);
        return update(record, wrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean batchSaveData(List<EnergyDataOriginalDO> checkedList) {
        return saveBatch(checkedList);
    }
}
